package com.itnature.testchip;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import org.apache.log4j.PropertyConfigurator;

import com.itnature.testchip.business.HeaderFooter;
import com.itnature.testchip.business.StopBin;
import com.itnature.testchip.business.TFDoc;
import com.itnature.testchip.business.TestSuite;
import com.itnature.testchip.business.Timing;
import com.itnature.testchip.business.VDDDetail;
import com.itnature.testchip.business.VDDSummary;
import com.itnature.testchip.process.ApplicationUtil;
import com.itnature.testchip.process.ExcelFileProcessing;
import com.itnature.testchip.process.TestChipProcessing;

public class MainClass {

	private static void initLog4j() {
		PropertyConfigurator.configure("log4j.properties");
	}

	public static void main(String[] args) throws Exception {
		initLog4j();

		String fileName = "SMP8674_75_FT_R1p0_TF.dat";
		String testFlowFile = "data16\\" + fileName;
		String levelFile = "data16\\SMP8674_75_R1p0_LEVELS.dat";
		String timingFile = "data16\\SMP8674_75_R1p0_TIMING.dat";
		String outputFilePath = "data16\\result.xls";

		TFDoc tfDoc = new TFDoc();

		String tfContent = ApplicationUtil.readFile(testFlowFile);

		
		// header footer
		HeaderFooter headerFooter = TestChipProcessing.getHeaderFooter(tfContent, fileName);
		String dataString = new SimpleDateFormat("MM/dd/yyyy").format(new Date());
		headerFooter.setDate(dataString);
		tfDoc.setHeaderFooter(headerFooter);
		// stopbin
		Vector<StopBin> stopBins = TestChipProcessing.getStopBins(tfContent);
		tfDoc.setStopBin(stopBins);
		// testsuite
		Vector<TestSuite> testSuites = TestChipProcessing.getTestSuites(
				tfContent, stopBins);
		tfDoc.setTestSuite(testSuites);

		// calculate timing from test suites
		tfContent = ApplicationUtil.readFile(timingFile);
		// timing
		Vector<Timing> timings = TestChipProcessing.getTimings(tfContent,
				testSuites);
		// calculate vdd is in next step
		tfContent = ApplicationUtil.readFile(levelFile);
		// System.out.println(tfContent);
		timings = TestChipProcessing.getVdd(tfContent, testSuites, timings);
		tfDoc.setTiming(timings);

		ExcelFileProcessing.createUniqueInstance(outputFilePath);
		ExcelFileProcessing.get().buildTFDOCSheet(tfDoc);
		// ExcelFileProcessing.get().flush();

		Vector<VDDSummary> vddSummaries = TestChipProcessing.getVddSummary(
				testSuites, timings);
		Vector<VDDDetail> vddDetail = TestChipProcessing.getVddDetail(
				tfContent, vddSummaries);

		ExcelFileProcessing.get().buildVDDSheet(vddSummaries, vddDetail);
		ExcelFileProcessing.get().flush();
	}

}
